//
//  WBEmoticonPageView.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/20.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBEmoticonPageView.h"
#import "WBEmoticonCell.h"
#import "WBEmoticonTool.h"

@interface WBEmoticonPageView () <UICollectionViewDataSource>

@end

@implementation WBEmoticonPageView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if(self = [super initWithFrame:frame collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]])
    {
    
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI
{
    self.backgroundColor = [UIColor randomColor];
    
    //设置数据源代理
    self.dataSource = self;
    
    //注册cell
    [self registerClass:[WBEmoticonCell class] forCellWithReuseIdentifier:@"item"];
    

}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //设置item大小
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    
    layout.itemSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
    
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

}

#pragma mark - 定义一个方法  供外界调用  滚动collection
- (void)selectSectionWithIndexPath:(NSIndexPath *)indexPath
{
    
    [self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionRight animated:NO];

}


#pragma mark - 数据源方法实现

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return [WBEmoticonTool sharedEmoticonTool].allEmoticons.count;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [WBEmoticonTool sharedEmoticonTool].allEmoticons[section].count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WBEmoticonCell *cell = (WBEmoticonCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    
    cell.index = indexPath;
    
    cell.emoticons = [WBEmoticonTool sharedEmoticonTool].allEmoticons[indexPath.section][indexPath.item];

    return cell;
}

@end
